package com.algorithms.recursives;

public class WordAnagram extends Thread {
	int totalCount = 0;

	public void run(){
		String input = "cathy";
		char[] charArr = input.toCharArray();
		anagram(charArr, 0);
	}
	
	public static void main(String args[]){
		new WordAnagram().start();
	}
	
	/**
	 * this will swap all characters in the string and 
	 * make up the combinations
	 * 
	 * @param charArr
	 */
	public void anagram(char[] charArr, int offs){
		char temp = ' ';
		if (offs == charArr.length - 1){
			System.out.println(++totalCount + ". " + new String(charArr));			
			return;
		}
		
		for (int i = offs; i < charArr.length; i++){
			// get the first item in the list
			temp = charArr[offs];
			
			// anagram the right-most n-1 array
			anagram(charArr, offs + 1);
			
			// after that move the rightmost n-1 array 
			// to the left 1 position
			for (int j = offs; j < charArr.length - 1; j++){
				charArr[j] = charArr[j + 1];
			}
			charArr[charArr.length - 1] = temp;
		}
	}
	
}
